<script>
import { mapState } from 'vuex'
import CargoForm from '@/views/business/cargo/components/cargoForm.vue'

export default {
  name: 'cargo',
  components: { CargoForm },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      edit: false
    }
  },
  methods: {
    /*
    *
    * 获取货品列表的回调函数
    *  */
    async fetchCargoList() {
      try {
        this.loading = true
        await this.$store.dispatch('cargoStore/fetchCargoListAction', '4061807061917973268')
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /*
    * 获取货物类型信息列表的回调函数
    *  */
    async fetchCargoType() {
      try {
        await this.$store.dispatch('cargoStore/fetchCargoTypeAction')
      } catch (error) {
        this.$message.error(error.message)
      }
    },

    /*
    * 删除货物的回调函数
    *  */
    async handleDelete(row) {
      try {
        const confirm = await this.$confirm('此操作将永久删除货物, 是否继续?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).catch(error => error)
        if (confirm === 'cancel') return
        const deleteResult = await this.$store.dispatch('cargoStore/deleteCargoAction', row.id)
        this.fetchCargoList()
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /*
    *
    * 新增货物的回调
    *  */
    handleAdd() {
      this.dialogVisible = true
      this.edit = false
    },
    /*
    *
    * 编辑货物的回调
    *  */
    handleEdit(row) {
      this.dialogVisible = true
      this.edit = true
      this.$nextTick(() => {
        this.$refs.cargoFormRef.getFormData(row)
      })
    },

    /*
    * 添加或者编辑货物
    *  */
    async handleSubmit() {
      if (this.edit) {
        try {
          const updateResult = await this.$store.dispatch('cargoStore/updateCargoAction', this.$refs.cargoFormRef.formData)
          this.dialogVisible = false
          this.fetchCargoList()
          this.$message.success(updateResult)
        } catch (error) {
          this.$message.error(error.message)
        }
      } else {
        try {
          // this.$refs.cargoFormRef.formData.id = '4061807061917973268'
          const addResult = await this.$store.dispatch('cargoStore/addCargoAction', this.$refs.cargoFormRef.formData)
          this.dialogVisible = false
          this.fetchCargoList()
          this.$message.success(addResult)
        } catch (error) {
          this.$message.error(error.message)
        }
      }

    }
  },
  mounted() {
    this.fetchCargoList()
    this.fetchCargoType()
  },
  computed: {
    ...mapState('cargoStore', ['cargoList']),
    title() {
      return this.edit ? '编辑货物' : '新增货物'
    }
  }
}
</script>

<template>
  <div class="cargo">
    <el-card shadow="never">
      <div class="header">
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">新增货物</el-button>
      </div>
      <el-table border stripe :data="cargoList" :header-cell-style="{background:'#f4f6f8',color:'#969393'}" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255,255,255)">
        <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
        <el-table-column prop="name" label="货品名称" align="center"></el-table-column>
        <el-table-column label="货品类型" align="center">
          <template #dfeault="{row}">{{ row.goodsType.name }}</template>
        </el-table-column>
        <el-table-column prop="quantity" label="货品数量(件)" align="center"></el-table-column>
        <el-table-column prop="totalWeight" label="重量(千克)" align="center"></el-table-column>
        <el-table-column prop="volume" label="体积(立方)" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogVisible">
      <cargo-form ref="cargoFormRef"></cargo-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.cargo {
  padding: 20px;

  .el-table {
    margin-top: 10px;
  }

}
</style>
